<?php
/**
 * 多级分类
 */
namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;

class Channel extends Model
{
    use ModelTree;
    use AdminBuilder;

    protected $table="channel";

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->setParentColumn('pid');
    }

    public function chindren()
    {
        return $this->hasMany('App\Models\Channel','pid','id');
    }

    /**
     * 获取该频道下的所有的子频道
     * Example
     *  [
     *     0 => [1, 2, 3],
     *     5 => [6, 7, 8]
     *  ]
     */
    public function childNode()
    {
        return array_map(function ($child){
            return $child['id'];
        },$this->chindren()->get()->toArray());
    }
}
